package org.sourcecode.common.cas.client;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

public abstract class SignCheckFilter extends AbstractSignFilter implements InitializingBean{
	
	public static final Set<String> UN_CHECK_URLS =  new HashSet<String>();
	
	public static  String TICKET;
	
	public static String SESSION_TICKET_NAME;
	
	public String ticket;
	
	public String session_ticket_name;
	
	private String un_check_urls ;
	
	private String signurl ;
	
	private String signdomain;
	
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		
		final HttpServletRequest request = (HttpServletRequest)req;
		final HttpServletResponse response = (HttpServletResponse) res;

		String redirectUrl = checkSignin(request, response);
		if (redirectUrl != null && isRequiredCheck(request)) {
			response.sendRedirect(redirectUrl);
			return ;
		}
		chain.doFilter(request, response);
		
	}
	
	protected boolean isRequiredCheck(HttpServletRequest request) {
		return true;
	}
	
	
	protected String checkSignin(HttpServletRequest request,HttpServletResponse response) {

		if(isSignin(request) == false && checkRequiredFilter(request)) {
			String service = getRedirectUrl(request, response, true,request.getMethod());
			String redirectUrl = signurl + ( signurl.contains("?") ? "&" : "?" ) + "service=" + service;;
			return redirectUrl;
		}
		return null;
	}
	
	public String getRedirectUrl(HttpServletRequest request,HttpServletResponse response,
			boolean isencode,String method) {
		String service = request.getRequestURL().toString();
		if("GET".equals(method)) {
			service = service + constuctQueryString(request);
		}
		try {
			if(isencode) {
				service = URLEncoder.encode(service, "GBK");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return service;
	}
	
	public String constuctQueryString(HttpServletRequest request) {
		String parameters = request.getQueryString();
		if(parameters != null) {
			return "?"+parameters;
		}
		return "";
	}
	
	public static boolean isSignin(HttpServletRequest request) {
		String session_ticket = (String)request.getSession().getAttribute(SESSION_TICKET_NAME);
		if (StringUtils.hasLength(session_ticket)) {
			return true;
		}
		return false;
	}
	
	public static boolean checkRequiredFilter(HttpServletRequest request) {
		String url  = request.getRequestURL().toString();
		for (String s:UN_CHECK_URLS) {
			if (url.indexOf(s) !=  -1) {
				return false;
			}
		}
		return true;
	}
	
	
	public void setUn_check_urls(String un_check_urls) {
		this.un_check_urls = un_check_urls;
	}


	public void afterPropertiesSet() throws Exception {
		initUncheckUrls();
		initTicketname();
		Assert.notNull(ticket);
		Assert.notNull(signdomain);
		
	}
	
	protected void initTicketname() {
		TICKET = ticket;
		SESSION_TICKET_NAME = session_ticket_name;
	}
	
	protected void initUncheckUrls() {
		try {
			if (un_check_urls != null) {
			 String [] urls  = un_check_urls.split(",");
			 for (String s:urls) {
				 UN_CHECK_URLS.add(s.trim());
			 }
		 }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void setSignurl(String signurl) {
		this.signurl = signurl;
	}


	public void setTicket(String ticket) {
		this.ticket = ticket;
	}


	public void setSigndomain(String signdomain) {
		this.signdomain = signdomain;
	}


	public String getSession_ticket_name() {
		return session_ticket_name;
	}


	public void setSession_ticket_name(String session_ticket_name) {
		this.session_ticket_name = session_ticket_name;
	}

	public String getSignurl(){
		return signurl;
	}


	
	
	

}
